package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Role;
import cn.wolfcode.crm.qo.EmployeeQuery;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.IRoleService;
import cn.wolfcode.crm.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeServiceImpl;
    @Autowired
    private IDepartmentService departmentServiceImpl;
    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"employee:list", "员工列表"}, logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") EmployeeQuery qo) {
        PageInfo<Employee> result = employeeServiceImpl.query(qo);
        List<Department> departments = departmentServiceImpl.listAll();
        model.addAttribute("result", result);
        model.addAttribute("depts", departments);
        return "/employee/list";
    }

    @RequestMapping("/input")
    @RequiresPermissions(value = {"employee:put", "员工新增编辑跳转页面"}, logical = Logical.OR)
    public String put(Model model, Long id) {
        //查询出全部角色 共享到编辑页面
        List<Role> roles = roleService.listAll();
        //查询出全部部门 共享到编辑页面
        List<Department> departments = departmentServiceImpl.listAll();
        model.addAttribute("depts", departments);
        model.addAttribute("roles", roles);
        if (id != null) {
            Employee employee = employeeServiceImpl.get(id);
            model.addAttribute("employee", employee);
        }
        return "/employee/input";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"employee:delete", "员工删除"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id) {
        employeeServiceImpl.delete(id);
        return new JsonResult("删除成功", true);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @RequestMapping("/btn-batchDelete")
    @RequiresPermissions(value = {"employee:batchDelete", "员工批量删除"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult batchDelete(Long[] ids) {
        employeeServiceImpl.batchDelete(ids);
        return new JsonResult("批量删除成功", true);
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"employee:saveOrUpdate", "员工新增编辑"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids) {
        if (employee.getId() != null) {
            employeeServiceImpl.update(employee, ids);
        } else {
            employeeServiceImpl.save(employee, ids);
        }
        return new JsonResult("新增或编辑成功", true);
    }

    /**
     * 用于远程验证用户名是否存在
     * 返回Map true代表验证通过(该用户名不存在) false代表验证不通过(用户名已经存在)
     *
     * @param name
     * @return
     */
    @RequestMapping("/checkName")
    @ResponseBody
    public Map<String, Boolean> checkName(String name) { //插件要求 valid:true
        Employee employee = employeeServiceImpl.checkName(name);
        HashMap<String, Boolean> map = new HashMap<>();
        map.put("valid", employee == null);
        return map;
    }

    @RequestMapping("/exportXls")
    public void exportXls(HttpServletResponse response) throws IOException {
        // 设置文件下载头
        response.setHeader("Content-disposition", "attachment;filename=employee.xls");
        // 创建 Workbook
        Workbook workbook = employeeServiceImpl.exportXls();
        // 输出到浏览器
        workbook.write(response.getOutputStream());

        // 以下代码 是用阿里巴巴easyexcel工具
//        List<Employee> employees = employeeServiceImpl.listAll();
//        EasyExcel.write(response.getOutputStream(), Employee.class).sheet("员工列表").doWrite(employees);
    }

    // 导入
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) {
        try {
            // 根据上传文件的输入流创建 Workbook
            Workbook wb = new HSSFWorkbook(file.getInputStream());
            employeeServiceImpl.importXls(wb);
            return new JsonResult("导入成功", true);
        } catch (IOException e) {
            e.printStackTrace();
            return new JsonResult("导入失败", false);
        }
    }


}
